<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单向链表</title>
</head>
<body>
<script>
    var Node= function (elm) {
        this.elm=elm;
        this.next=null;
    };


    function List(){
        this.head=new Node('head');
    }
    List.prototype={
        constructor:List,
        each: function (cb) {
            var curr=this.head;
            do{
                cb(curr);
            }while(curr=curr.next);
        },
        find: function (item) {
            var curr=this.head;
            do{
                if(curr.elm==item){
                    return curr;
                }
            }while(curr=curr.next);
            return null;
        },
        insert: function (newElm,target) {
            var curr=this.find(target),newNode;
            if(!curr)return false;
            newNode=new Node(newElm);
            newNode.next=curr.next;
            curr.next=newNode;
            this[newElm]=newNode;
            return true;
        },
        display: function () {
            console.info('display:');
            return console.log(this);
        },
        prev: function (elm) {
            var curr=this.head;
            do{
                if(curr.next&&curr.next.elm==elm){
                    return curr;
                }
            }while(curr=curr.next);
            return null;
        },
        remove: function (elm) {
            var prev=this.prev(elm);
            if(!prev)return false;
            prev.next=prev.next.next;
            delete this[elm];
            return true;
        }
    };

    var list=new List();
    console.log('list:',list);

    list.insert('first','head');
    list.insert('second','head');
    list.insert('third','head');

    list.display();

    console.log('find first:',list.find('first'));

    console.log('remove first:',list.remove('first'));
    list.display();


</script>
</body>
</html>